<template>
	<view class="page">
		<view class="bar_info" :style="{height:(88+StatusBarRpx)+'rpx',paddingTop:StatusBarRpx+'rpx'}">
			<view class="title_bar">
				<view class="title_bar_left" @tap.stop="handleJump" data-type="BACK">
					<image class="title_bar_left_icon" :src='STATIC_URL+"5.png"'></image>
				</view>
				<view class="title_bar_center">
					我的收藏
				</view>
				<view class="title_bar_right"></view>
			</view>
		</view>
		<view :style="{height: (88+StatusBarRpx)+'rpx'}"></view>
		<view class="loading_css" v-if="isShow">
			<u-loading size="36" color="#41AF36"></u-loading>
			<view class="loading_text">加载中</view>
		</view>
		<template v-else>
			<view class="shop_list">
				<view class="shop_item flex align-center" v-for="(item, index) in list" :key="index">
					<view class="select_icon flex align-center" v-if="is_manage" @tap.stop="selectShop(index)">
						<image v-if="item.isSelect" :src='STATIC_URL+"2.png"'></image>
						<image v-else :src='STATIC_URL+"3.png"'></image>
					</view>
					<view class="select_view flex" :class="!is_manage ? 'width100' : ''" @tap.stop="handleJump" data-type="navigateTo" :data-url="`/pages/index/goodDetails?id=${item.product_id}`">
						<view class="item_left_img">
							<u-lazy-load :image="item.image" img-mode="aspectFill" height="180" border-radius="8"></u-lazy-load>
						</view>
						<view class="item_right">
							<view class="shop_name flex align-center">
								<view class="shop_name_text">{{item.name}}</view>
								<image class="collect_icon" :src='STATIC_URL+"43.png"'></image>
							</view>
							<view class="shop_price flex align-center justify-between" v-if="user.is_show_price == 1">
								<view class="price_text">
									<text class="price_1">￥</text>
									<text class="price_2">{{item.price}}</text>
									<!-- <text class="price_3">/包</text> -->
								</view>
							</view>
						</view>
					</view>
					
				</view>
				<u-loadmore v-if="last_page > 1" class="loadmore" font-size="24" :status="status"
					:icon-type="iconType" :load-text="loadText" />
			</view>
			<view class="noData" v-if="list.length == 0 && !isShow">
				<image src="/static/empty/no.png"></image>
			</view>
			<view class="btn_info" v-if="!is_manage && list.length > 0" @tap.stop="is_manage = true">
				<view class="save_btn">管理</view>
			</view>
			<view class="operate_btn flex align-center justify-between" v-if="is_manage && list.length > 0">
				<view class="operate_1 flex align-center" @tap.stop="selectAll">
					<image v-if="is_total" :src='STATIC_URL+"2.png"'></image>
					<image v-else :src='STATIC_URL+"3.png"'></image>
					<text>全选</text>
				</view>
				<view class="operate_2 flex align-center justify-center" @tap.stop="delCollection">删除</view>
			</view>
		</template>
		
	</view>
</template>

<script>
	import {
		validate
	} from '@/common/utils/validate.js'
	export default {
		data() {
			return {
				image: '/static/index/3.png',
				body: "",
				isShow: true,
				is_manage: false,
				is_total: false,
				product_id: [],
				list: [],
				"page": 1,
				"is_pull": true,
				"last_page": 1,
				"status": 'loadmore',
				"iconType": 'flower',
				"loadText": {
					loadmore: '轻轻上拉',
					loading: '努力加载中',
					nomore: '我也是有底线的'
				},
				user: {},
				
			}
		},
		onLoad() {
			this.getCollectList();
		},
		onShow() {
			this.user = global.userInfo;
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.page = 1;
			this.is_pull = true;
			this.isShow = true;
			this.getCollectList();
		},
		// 上拉加载
		onReachBottom() {
			if (this.page >= this.last_page) return;
			this.status = 'loading';
			this.page++;
			setTimeout(() => {
		
				if (this.page >= this.last_page) this.status = 'nomore';
				else this.status = 'loading';
				this.is_pull = false;
				this.getCollectList();
			}, 500)
		},
		methods: {
			getCollectList() {
				this.$api.get(global.apiUrls.gylCollection, {
					page: this.page,
					pageSize: 10
				}).then(res => {
					// console.log(res);
					uni.stopPullDownRefresh();
					this.isShow = false;
					if (res.data.code == 200) {
						if (res.data.data.items.length > 0) {
							res.data.data.items.forEach((v, k) => {
								v.isSelect = false;
							})
						}
						this.last_page = res.data.data.pageInfo.totalPage;
						if (this.is_pull) {
							this.list = res.data.data.items;
						} else {
							this.list = this.list.concat(res.data.data.items);
						}
					} else {
						this.$message.info(res.data.message);
					}
				})
			},
			// 删除收藏
			delCollection() {
				if (this.product_id.length == 0) {
					this.$message.info('请选择商品');
					return;
				}
				this.$api.post(global.apiUrls.gylAddCollection, {
					product_id: this.product_id.join(','),
					type: 2
				}).then(res => {
					this.$message.info(res.data.message);
					if (res.data.code == 200) {
						this.isShow = true;
						this.page = 1;
						this.is_pull = true;
						this.isShow = true;
						this.getCollectList();
					}
				})
			},
			
			selectAll() {
				this.product_id = [];
				if (this.is_total) {
					this.is_total = false;
					this.list.forEach((v, k) => {
						v.isSelect = false;
					})
				} else {
					this.is_total = true;
					this.list.forEach((v, k) => {
						v.isSelect = true;
						this.product_id.push(v.product_id);
					})
				}
			},
			selectShop(index) {
				this.list[index].isSelect = !this.list[index].isSelect;
				let num = -1;
				this.list.forEach((v, k) => {
					if (!v.isSelect) {
						num = k;
					}
				})
				if (num == -1) {
					this.is_total = true;
				} else {
					this.is_total = false;
				}
				this.product_id = [];
				this.list.forEach((v, k) => {
					if (v.isSelect) {
						this.product_id.push(v.product_id);
					}
				})
			},
			filterDictionary(id) {
				let item = global.dictionary.filter(item => item.id == id);
				if (item.length > 0) {
					return item[0].name;
				} else {
					return "未知";
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		background: #f5f5f5;
		background-size: 100% auto !important;
		.bar_info {
			width: 750rpx;
			height: 88rpx;
			overflow: hidden;
			z-index: 10;
			position: fixed;
			top: 0rpx;
		
			.title_bar {
				padding: 0 32rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
		
				.title_bar_left {
					width: 50rpx;
					.title_bar_left_icon {
						width: 18rpx;
						height: 32rpx;
					}
				}
		
				.title_bar_center {
					line-height: 88rpx;
					font-size: 36rpx;
					font-weight: 400;
				}
		
				.title_bar_right {
					width: 50rpx;
				}
			}
		}
		
	}
	.btn_info {
		width: 100%;
		height: 110rpx;
		position: fixed;
		bottom: 0;
		padding: 15rpx 25rpx;
		z-index: 10;
		.save_btn {
			width: 700rpx;
			height: 80rpx;
			background: #41AF36;
			border-radius: 50rpx;
			font-size: 30rpx;
			font-weight: 500;
			color: #fff;
			text-align: center;
			line-height: 80rpx;
			
		}
	}
	.operate_btn {
		width: 100%;
		height: 110rpx;
		position: fixed;
		bottom: 0;
		padding: 15rpx 25rpx;
		z-index: 10;
		background: #fff;
		.operate_1 {
			font-weight: 400;
			font-size: 26rpx;
			color: #A9A9A9;
			image {
				width: 40rpx;
				height: 40rpx;
				margin-right: 20rpx;
			}
		}
		.operate_2 {
			width: 214rpx;
			height: 80rpx;
			background: #41AF36;
			border-radius: 50rpx;
			font-weight: 500;
			font-size: 30rpx;
			color: #FFFFFF;
		}
	}
	.shop_list {
		padding: 20rpx 20rpx 0;
		.shop_item {
			background: #fff;
			border-radius: 10rpx;
			padding: 30rpx 20rpx;
			margin-bottom: 20rpx;
			.select_icon {
				width: 50rpx;
				image {
					width: 40rpx;
					height: 40rpx;
				}
			}
			.width100 {
				width: 100% !important;
			}
			.select_view {
				width: calc(100% - 50rpx);
				.item_left_img {
					width: 180rpx;
					height: 180rpx;
				}
				/deep/.u-lazy-item {
					width: 180rpx !important;
					height: 180rpx !important;
				}
				.item_right {
					width: calc(100% - 180rpx);
					padding: 0 0 0 30rpx;
					.shop_name {
						.shop_name_text {
							width: calc(100% - 40rpx);
							font-weight: 500;
							font-size: 32rpx;
							color: #3D3D3D;
							text-overflow: ellipsis;
							overflow: hidden;
							white-space: nowrap;
						}
						
						.collect_icon {
							width: 40rpx;
							height: 40rpx;
						}
					}
					.shop_price {
						margin-top: 50rpx;
						.price_text {
							.price_1 {
								color: #D43C3B;
								font-size: 24rpx;
								font-weight: 600;
							}
							.price_2 {
								color: #D43C3B;
								font-size: 40rpx;
								font-weight: 600;
							}
							.price_3 {
								color: #989898;
								font-size: 24rpx;
								font-weight: 500;
							}
						}
						.shop_num {
							image {
								width: 60rpx;
								height: 40rpx;
							}
							.shop_num_input {
								text-align: center;
								margin: 0 16rpx;
								font-weight: 400;
								font-size: 30rpx;
								color: #3D3D3D;
							}
						}
					}
				}
			}
		}
	}
</style>